package com.ycy.temperature.view.fragment;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.utils.JUtils;
import com.ycy.temperature.R;
import com.ycy.temperature.adapter.DynamicAdapter;
import com.ycy.temperature.bean.DynamicBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.view.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;

/**
 * Created by MI on 2018/7/24.
 */

public class DynamicMeFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    BaseQuickAdapter mAdapter;
    List<DynamicBean.DataBean> mData = new ArrayList<>();
    int i = 0;

    @Override
    protected int getRes() {
        return R.layout.fragment_dynamic;
    }

    @Override
    protected void init(View view) {
        ButterKnife.bind(this, view);
        initView();
    }

    @Override
    protected void setData(View view) {

    }

    @Override
    public void onClick(View v) {

    }

    private void initView() {
        mAdapter = new DynamicAdapter(R.layout.adapter_dynamic, mData);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        setDynamic("0");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void setDynamic(final String str) {
        Log.e("strasdasd", str);
        UserNetWorks.setDynamic("1", "1", str, "10", new Subscriber<DynamicBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
                if (Integer.valueOf(str) == TYPE_REFRESH) {
                    //刷新
                    mAdapter.setEnableLoadMore(true);
                } else {
                    //加载更多
                    mAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onNext(DynamicBean dynamicBean) {
                if (dynamicBean.getData() != null && dynamicBean.getData().size() > 0) {
                    i++;
                }
                if (dynamicBean.getResponseState().equals("1")) {
                    if (dynamicBean.getData() != null) {
                        List<DynamicBean.DataBean> newList = dynamicBean.getData();
                        if (newList != null && newList.size() > 0) {
                            if (Integer.valueOf(str) == TYPE_REFRESH) {
                                //刷新
                                mAdapter.setNewData(newList);
                                mAdapter.setEnableLoadMore(true);
                            } else {
                                //加载更多
                                mAdapter.addData(newList);
                                mAdapter.loadMoreComplete();
                            }
                        } else {
                            if (Integer.valueOf(str) == TYPE_REFRESH) {
                                //刷新
                                mAdapter.setEnableLoadMore(true);
                            } else {
                                //加载更多
                                mAdapter.loadMoreEnd();
                            }
                        }
                    }
                }
            }
        });
    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

}
